package com.css.fxfzypg.modules.baseData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.YNEnum;
import com.css.fxfzypg.modules.baseData.entity.PbeEntity;
import com.css.fxfzypg.modules.baseData.queryParams.PbeQueryParams;
import com.css.fxfzypg.modules.baseData.repository.PbeRepository;
import com.css.fxfzypg.modules.baseData.service.PbeService;
import com.css.fxfzypg.modules.baseData.vo.PbeEntityVO;
import com.css.fxfzypg.util.FileUtil;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformPageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-08-11 9:34
 */
@Service
public class PbeServiceImpl implements PbeService {

    @Resource
    public PbeRepository pbeRepository;

    @Override
    public PbeEntity fingPbeById(String id) {
        return pbeRepository.findById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PbeEntity savePbe(PbeEntity pbeEntity) {

        pbeEntity.setIsdelete(YNEnum.Y.toString());
        pbeEntity.setCreattime(new Date());
      return   pbeRepository.savePbe(pbeEntity);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PbeEntity updatePbe(PbeEntity pbeEntity) {
        pbeEntity.setCreattime(new Date());
        pbeEntity.setIsdelete(YNEnum.Y.toString());
        return pbeRepository.updatePbe(pbeEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePbe(List<String> ids) {
        pbeRepository.deletePbe(ids);
    }

    @Override
    public JSONObject getAllPbeByParams(PbeQueryParams queryParams) {
        return PlatformPageUtils.formatPageData(pbeRepository.getAllPbeByParams(queryParams));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<PbeEntity> list = pbeRepository.exportExcel();
        FileUtil.exportExcel(list, "人口建筑物经济公里格网数据", "sheet", PbeEntity.class, "PbeExcel", response);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importExcel(MultipartFile file)throws IOException {

        List<PbeEntity> pbeEntities = FileUtil.importExcel(file, PbeEntity.class);
        for (PbeEntity pbeEntity : pbeEntities) {
            pbeEntity.setCreattime(PlatformDateUtils.getCurrentTimestamp());
            pbeEntity.setIsdelete(YNEnum.Y.toString());

        }
        pbeRepository.importExcel(pbeEntities);
    }

//    @Override
//    public List<PbeEntity> getPbeListByAreaCode(List<String> cityCodeList) {
//        if(PlatformObjectUtils.isNotEmpty(cityCodeList)){
//            return pbeRepository.getPbeListByAreaCode(cityCodeList);
//        }
//        return null;
//    }


    @Override
    public PbeEntity fingPbeByPbeId(String pbeId) {
        return pbeRepository.fingPbeByPbeId(pbeId);
    }

    @Override
    public List<PbeEntity> getPbeByInfluenceflieId(String id) {
        return pbeRepository.getPbeByInfluenceflieId(id);
    }

    @Override
    public List<PbeEntityVO> getPbe2ByInfluenceflieId(String id) {
        return pbeRepository.getPbe2ByInfluenceflieId(id);
    }

    @Override
    public List<PbeEntity> getPbeInfo(String prefecture, String code, String taskId) {

        return pbeRepository.getPbeInfo(prefecture,code,taskId);
    }
    @Override
    public List<PbeEntity> getPbeInfo1(String taskId) {
        return pbeRepository.getPbeInfo1(taskId);
    }

    @Override
    public List<PbeEntityVO> getPbe2ByInfluenceflieIdAndTaskId(String influencefliedId, String taskId) {
        return pbeRepository.getPbe2ByInfluenceflieIdAndTaskId(influencefliedId,taskId);
    }
}
